<template>
    <div class="login-container">
      <el-card class="login-card">
        <template #header>
          <h2>用户登录</h2>
        </template>
        
        <el-form :model="loginForm" label-position="top">
          <el-form-item label="账号">
            <el-input 
              v-model="loginForm.username"
              placeholder="请输入账号"
              clearable
            />
          </el-form-item>
  
          <el-form-item label="密码">
            <el-input
              v-model="loginForm.password"
              type="password"
              placeholder="请输入密码"
              show-password
              clearable
            />
          </el-form-item>
  
          <el-form-item label="登录有效期">
            <el-select 
              v-model="loginForm.tokenMaxAge" 
              placeholder="请选择登录有效期"
              style="width: 100%"
            >
              <el-option label="7天" :value="7" />
              <el-option label="14天" :value="14" />
              <el-option label="30天" :value="30" />
            </el-select>
          </el-form-item>
  
          <el-form-item>
            <el-button type="primary" @click="handleLogin" style="width: 100%">
              登录
            </el-button>
          </el-form-item>
  
          <div class="login-links">
            <router-link to="{ name: 'UserRegister' }">还没有账号？立即注册</router-link>
          </div>
        </el-form>
      </el-card>
    </div>
  </template>
  
  <script setup>
  import { ref } from 'vue'
  import { useRouter } from 'vue-router'
  import { login } from '@/api/user'
  import { ElMessage } from 'element-plus'
  
  const router = useRouter()
  const loginForm = ref({
    username: '',
    password: '',
    tokenMaxAge: 7
  })
  
  const handleLogin = async () => {
    try {
      if (!loginForm.value.username || !loginForm.value.password) {
        ElMessage.warning('请填写完整的登录信息')
        return
      }
  
      await login(
        loginForm.value.username,
        loginForm.value.password,
        loginForm.value.tokenMaxAge
      )
      
      router.push('/') // 登录成功后跳转到首页
    } catch (error) {
      console.error('登录失败:', error)
    }
  }
  </script>
  
  <style scoped>
  .login-container {
    display: flex;
    justify-content: center;
    align-items: center;
    min-height: 100vh;
    width: 100%;
    background-color: #f5f7fa;
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
  }

  .login-card {
    width: 400px;
    margin: 20px;
    box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
  }

  
  .login-card :deep(.el-card__header) {
    text-align: center;
    padding: 20px;
  }
  
  .login-card h2 {
    margin: 0;
    color: #303133;
  }
  
  .login-links {
    text-align: center;
    margin-top: 16px;
  }
  
  .login-links a {
    color: #409eff;
    text-decoration: none;
  }
  
  .login-links a:hover {
    color: #66b1ff;
  }
  </style>